<?php
require_once 'BaseController.php';

/**
 * 【PC版】Ajaxエリア検索 コントローラ
 */
class Renew_AreaajaxController extends Renew_BaseController
{

    /**
     * 駅取得アクション
     */
    public function getekiAction()
    {
        // makes disable renderer
        $this->_helper->viewRenderer->setNoRender();

        $params = $this->getParameters();

        $eki = $params['eki'];

        $model = new Search_Model_Selareasearch();

        $like = ($params['likeop'] == '1' ? true : false);

        $data = $model->getAjaxEki($eki, $like);

        $ret = array();
        foreach ($data as $val)
        {
            $pref = $this->getPrefNameJP($val['TODOUFUKEN_CD']);
            $ret[] = $val['EKI_NAME'] . '(' . $pref . ')' . '|' . $val['TODOUFUKEN_CD'] . '-' . str_replace(',', '.', $val['FULL_EKI_CD']);
        }
        // echo json_encode($data);
        echo implode(';', $ret);
        exit();
    }

    // 駅名検索、路線図CDを返すAjaxメソッド
    // jschoi 20141221
    public function getekirosencdAction()
    {
        // makes disable renderer
        $this->_helper->viewRenderer->setNoRender();

        $params = $this->getParameters();

        $eki = $params['eki'];

        $model = new Search_Model_Selareasearch();

        $like = ($params['likeop'] == '1' ? true : false);

        $data = $model->getAjaxEkiRosenCode($eki, $like);

        $ret = array();
        foreach ($data as $val)
        {
            $pref = $this->getPrefNameJP($val['TODOUFUKEN_CD']);
            $ret[] = $val['EKI_NAME'] . '(' . $pref . ')' . '|' . $val['TODOUFUKEN_CD'] . '-' . str_replace(',', '.', $val['FULL_ROSENZU_EKI_CD']) . '|' . $val['TODOUFUKEN_CD'] . '-' . str_replace(',', '.', $val['FULL_EKI_CD']);
        }
        // echo json_encode($data);
        echo implode(';', $ret);
        exit();
    }

    // セッションに保存した代表建物コードを削除
    // TODO: alias のはりかたがわからないから使わなくなったものを使いまわしている。修正して。
    public function getmapAction()
    {
        $params = $this->getParameters();
        if (isset($params['reqKey']))
        {
            unset($_SESSION["MAPDATA_{$params['reqKey']}"]);
        }
        echo '{"msg":"OK"}';
        exit();
    }

    public function array2json($arrList)
    {
        $ret = array();

        foreach ($arrList as $arr)
        {
            foreach ($arr as $key => $value)
            {
                if (is_array($value))
                { // Custom handling for arrays
                    if ($is_list)
                        $parts[] = array2json($value); /* :RECURSION: */
                    else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
                }
                else
                {
                    $str = '';
                    if (!$is_list) $str = '"' . $key . '":';

                    // Custom handling for multiple data types
                    // if(is_numeric($value)) $str .= $value; //Numbers
                    if ($value === false)
                        $str .= 'false'; // The booleans
                    elseif ($value === true)
                        $str .= 'true';
                    else $str .= '"' . addslashes($value) . '"'; // All other things
                                                                 // :TODO: Is there any more
                                                                 // datatype we should be in the
                                                                 // lookout for? (Object?)

                    $parts[] = $str;
                }
            }
            $ret[] = '{' . implode(',', $parts) . '}';
        }

        $val = '[' . implode(',', $ret) . ']'; // Return numerical JSON
        return '{"items":' . $val . '}'; // Return associative JSON
    }

}
?>
